#include "../include/SDLLib.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

#include <stdexcept>
#include <string>
#include <stdlib.h>

namespace GUL {
    namespace System {
        namespace Graphic {

            SDLLib::SDLLib(Uint32 flag) {
                //Initialize SDL
                if (SDL_Init(flag) < 0)
                    throw std::runtime_error("SDL could not initialize! SDL_Error: " + std::string(SDL_GetError()) + "\n");
                SDL_ClearError();
                atexit(SDL_Quit);
            }

            SDLLib::SDLLib() {
                //Initialize SDL
                //    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
                //        throw std::runtime_error("SDL could not initialize! SDL_Error: " + std::string(SDL_GetError()) + "\n");
                //    SDL_ClearError(); 
                atexit(SDL_Quit);
            }

            SDLLib::~SDLLib() {

            }

            void SDLLib::InitTTFSupport() {
                if (TTF_Init() == -1)
                    throw std::runtime_error("Couldn't not initiate TTF Support Library TTF Error: " + std::string(TTF_GetError()));
            }

            void SDLLib::InitVideoSupport() {
                if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1)
                    throw std::runtime_error("Couldn't not initiate VideoSubsystem. SDL Error: " + std::string(SDL_GetError()));
            }

            void SDLLib::InitAudioSupport() {
                if (SDL_InitSubSystem(SDL_INIT_AUDIO) == -1)
                    throw std::runtime_error("Couldn't not initiate AudioSubsystem. SDL Error: " + std::string(SDL_GetError()));
            }

            void SDLLib::InitEventsSupport() {
                if (SDL_InitSubSystem(SDL_INIT_EVENTS) == -1)
                    throw std::runtime_error("Couldn't not initiate EventsSubsystem. SDL Error: " + std::string(SDL_GetError()));
            }

            ///

            void SDLLib::QuitTTFSupport() {
                TTF_Quit();
            }

            void SDLLib::QuitVideoSupport() {
                SDL_QuitSubSystem(SDL_INIT_VIDEO);
            }

            void SDLLib::QuitAudioSupport() {
                SDL_QuitSubSystem(SDL_INIT_AUDIO);
            }

            void SDLLib::QuitEventsSupport() {
                SDL_QuitSubSystem(SDL_INIT_EVENTS);
            }

            ///

            bool SDLLib::isTTFSupportEnabled() {
                if (TTF_WasInit() != 0)
                    return true;
                return false;
            }

            bool SDLLib::isVideoSupportEnabled() {
                if (SDL_WasInit(SDL_INIT_VIDEO) != 0)
                    return true;
                return false;
            }

            bool SDLLib::isAudioSupportEnabled() {
                if (SDL_WasInit(SDL_INIT_AUDIO) != 0)
                    return true;
                return false;
            }

            bool SDLLib::isEventsSupportEnabled() {
                if (SDL_WasInit(SDL_INIT_EVENTS) != 0)
                    return true;
                return false;
            }


        }
    }

}